/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//Ext.apply(Ext.form.VTypes, {
//
//    password : function(val, field)
//	{
//        if (field.initialPassField) {
//            var login = Ext.getCmp(field.initialPassField);
//            return (val == login.getValue());
//        }
//        return true;
//    },
//    passwordText : 'Passwords do not match' //alert if you enter a password that is not the same
//});

Ext.apply(Ext.form.VTypes, {
   password: function(value, field)
   {
      if (field.initialPasswordField)
      {
         var pwd = Ext.getCmp(field.initialPasswordField);
         this.passwordText = 'Confirmation does not match your intial password entry.';
         return (value == pwd.getValue());
      }

      this.passwordText = 'Passwords must be at least 5 characters, containing either a number, or a valid special character (!@#$%^&*()-_=+)';

      var hasSpecial = value.match(/[0-9!@#\$%\^&\*\(\)\-_=\+]+/i);
      var hasLength = (value.length >= 5);

      return (hasSpecial && hasLength);
   },

   passwordText: 'Passwords must be at least 5 characters, containing either a number, or a valid special character (!@#$%^&*()-_=+)'
});

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
Ext.onReady(onReadyCall);
var panelusuario;
function onReadyCall()
{
   readControls_Login();


     var txtRuc = new Ext.form.NumberField({
        id: 'txtRuc',
        fieldLabel: 'Ruc',
        width: 250,
        align:'left',
        allowNegatives: false,
        convertToUpperCase: true ,
        allowBlank:false,
        maxLength: 11,
        minLength:11
    });
     // txtdni.render('Divdni');

      var txtRazonSocial = new Ext.form.TextField({
        id: 'txtRazonSocial',
         fieldLabel: 'RazonSocial',
        allowBlank:false,
        width:250
    });
     // txtNombres.render('Divnombres');

       var txtNroCuenta = new Ext.form.TextField({
        id: 'txtNroCuenta',
         fieldLabel: 'Nro. Cuenta',
        allowBlank:true,
        width:250
    });
    //  txtApellidos.render('Divapellidos');

     var txtDireccion = new Ext.form.TextField({
        id: 'txtDireccion',
        fieldLabel: 'Direccion',
        allowBlank:true,
        width:250
    });
    //  txtDireccion.render('Divdireccion');

     var txtContacto = new Ext.form.TextField({
        id: 'txtContacto',
         fieldLabel: 'Contacto',
        allowBlank:false,
        width:250
    });
     // txtTelefono.render('Divtelefono');

       var txtClave = new Ext.form.TextField({
           fieldLabel: 'Clave',
           id: 'txtClave',
           allowBlank: false,
           inputType: 'password',
           vtype: 'password',
           width: 250,
           maxLength: 20
    });
      var txtClaveconfirm = new Ext.form.TextField({
           fieldLabel: 'Confirmar Clave',
           id: 'txtClaveconfirm',
           inputType: 'password',
           vtype: 'password',
           allowBlank: false,
           width: 250,
           maxLength: 20,
           initialPasswordField: 'txtClave'
    });
     // txtClave.render('Divclave');

       var txtEmail = new Ext.form.TextField({
        id: 'txtEmail',
        allowBlank:false,
         fieldLabel: 'Email',
        width:250,
        vtype: 'email'
    });

    // btnLimpiar.render('Divbtnlimpiar');
    panelusuario = new Ext.form.FormPanel({
                xtype: 'panel',
                frame:true,
                region:'center',
                 title: 'Registro Empresa',
                layout: 'form',
                bodyStyle:'padding:15px 15px 0',
                width: 600,
                items: [
                      {
                        layout:'column',
                        items:[
                        {
                            columnHeight:.10,
                            layout: 'form'
                        }
                         ]
                    } ,
                     txtRuc,
                     txtRazonSocial,
                     txtContacto,
                     txtDireccion,
                     txtNroCuenta,
                     txtClave,
                     txtClaveconfirm,
                     txtEmail
                ],
                   buttons: [{
                    text: 'Registrar',
                    id:'btnregistrar',
                    align: 'center',
                    handler: function()
                    {
                        if(!panelusuario.getForm().isValid())
                        {
                          return false;
                        }
                        var v_Ruc = Ext.get('txtRuc').dom;
                        var v_RazonSocial = Ext.get('txtRazonSocial').dom;
                        var v_Contacto = Ext.get('txtContacto').dom;
                        var v_direccion = Ext.get('txtDireccion').dom;
                        var v_NroCuenta = Ext.get('txtNroCuenta').dom ;
                        var v_clave = Ext.get('txtClave').dom ;
                        var v_confirm = Ext.get('txtClaveconfirm').dom ;
                        var v_email = Ext.get('txtEmail').dom ;

                         if(v_Ruc.value =="")
                        {
                            v_Ruc.focus();
                            Ext.MessageBox.alert('Mensaje', 'Campo incorrecto!.');
                            return false;
                        }
                         if(v_RazonSocial.value =="")
                        {
                            v_RazonSocial.focus();
                            Ext.MessageBox.alert('Mensaje', 'Campo incorrecto!.');
                            return false;
                        }
                        if(v_Contacto.value =="")
                        {
                            v_Contacto.focus();
                            Ext.MessageBox.alert('Mensaje', 'Campo incorrecto!.');
                            return false;
                        }

                        if(v_email.value =="")
                        {
                            v_email.focus();
                            Ext.MessageBox.alert('Mensaje', 'Campo incorrecto!.');
                            return false;
                        }
   /**********************************/
     Ext.MessageBox.show({
       msg: 'Guardando los Datos , por favor espere...',
       progressText: 'Guardando...',
       width:300,
       wait:true//,
       //waitConfig: {interval:200}//,
       //icon:'ext-mb-download', //custom class in msg-box.html
       //animEl: 'mb7'
 });
    var AddResultItinerarios = new Ext.data.Connection();
    AddResultItinerarios.request({
        url: URLWebService + '/GrabaEmpresa',
        params: {
                ruc:  v_Ruc.value.replace(/^\s*|\s*$/g,""),
                razon:   v_RazonSocial.value.replace(/^\s*|\s*$/g,""),
                contacto:   v_Contacto.value.replace(/^\s*|\s*$/g,""),
                direccion:   v_direccion.value ,
                nrocuenta:   v_NroCuenta.value.replace(/^\s*|\s*$/g,"") ,
                clave:   v_clave.value ,
                email:   v_email.value
                },
        method: 'POST',
        scope: this,
        callback: function(options, success, response){
        if (success){
             Ext.MessageBox.hide();
            var xml = response.responseText;
            var re = /\n/;
            xml = xml.replace(re, '');
            re = /\r/;
            xml = xml.replace(re, '');
            var ObjectRes =  Ext.decode(xml);
           // var ObjectRes= Ext.util.JSON.encode(xml);
            ObjectRes= ObjectRes.MensajeResultado;
            var return_Estado = ObjectRes.ESTADO;
            var return_Mensaje = ObjectRes.MENSAJE ;
            Ext.MessageBox.show({
               title: 'Resultado',
               msg: return_Mensaje,
               buttons: Ext.MessageBox.OK,
               fn: function(){
                if(return_Estado == 1)
                {
                  LimpiarPanel();
                   location.reload();
                }else
                {

                }

               }
           });
      }
      }
    });
   /********************************/
                    }
                    }, {
                    text: 'Limpiar',
                    handler: function(){
                        LimpiarPanel();
                    }
                 }]
            });
             panelusuario.render('Divusuario');
}

function LimpiarPanel()
{
    Ext.get('txtRuc').dom.value = "";
    Ext.get('txtRazonSocial').dom.value = "";
    Ext.get('txtContacto').dom.value = "";
    Ext.get('txtDireccion').dom.value = "";
    Ext.get('txtNroCuenta').dom.value = "";
    Ext.get('txtClave').dom.value = "";
    Ext.get('txtClaveconfirm').dom.value = "";
    Ext.get('txtEmail').dom.value = "";

}


